//
//  LXSteper.m
//  绿稀全球购
//
//  Created by 王敏 on 16/6/5.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXSteper.h"

@interface LXSteper()

@property(nonatomic,strong)UIButton * addButton;
@property(nonatomic,strong)UIButton * reduceButton;
@property(nonatomic,strong)UILabel * countLabel;

/**
 *  steper 当前值
 */
//@property(nonatomic,assign)NSInteger steperValue;
@end
@implementation LXSteper


-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = HexColor(@"#EEEEEE").CGColor;
        [self addRadius:2];
        
        UIButton * addButton = [[UIButton alloc] init];
        [self addSubview:addButton];
        [addButton setTitle:@"+" forState:UIControlStateNormal];
        [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        addButton.layer.borderWidth = 0;
        [addButton addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.addButton = addButton;
        
        UILabel * countLabel = [[UILabel alloc] init];
        countLabel.textAlignment = NSTextAlignmentCenter;
        countLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:countLabel];
        countLabel.layer.borderWidth = 0.5;
        countLabel.layer.borderColor = HexColor(@"#EEEEEE").CGColor;
        self.countLabel = countLabel;
        
        UIButton * reduceButton = [[UIButton alloc] init];
        [self addSubview:reduceButton];
        [reduceButton setTitle:@"-" forState:UIControlStateNormal];
        [reduceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [reduceButton addTarget:self action:@selector(reduceButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        reduceButton.layer.borderWidth = 0;
        self.reduceButton = reduceButton;
        self.steperValue = 1;
        
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.reduceButton.width     = self.width*0.3;
    self.reduceButton.height    = self.height;
    self.reduceButton.x         = 0.5;
    self.reduceButton.y         = 0;


    self.addButton.width  = self.width*0.3;
    self.addButton.height = self.height;
    self.addButton.x  = (self.width-self.addButton.width);
    self.addButton.y      = 0;
    
    self.countLabel.width    = self.addButton.left-self.reduceButton.right+1;
    self.countLabel.height   = self.height;
    self.countLabel.x        = self.reduceButton.right-0.5;
    self.countLabel.y        = 0;
}


-(void)addButtonClick:(UIButton*)button{
    if(self.addCallBack) self.addCallBack(self);
}

-(void)reduceButtonClick:(UIButton*)button{
    if(self.reduceCallBack) self.reduceCallBack(self);
}

-(void)add
{
    self.steperValue ++;
}

-(void)reduce
{
    self.steperValue = self.steperValue-1;
}

-(void)setSteperValue:(NSInteger)steperValue{
    if (steperValue<0) {
        steperValue = 0;
    }
    _steperValue = steperValue;
    self.countLabel.text = [NSString stringWithFormat:@"%ld",self.steperValue];
    if(self.handle){
        self.handle(self.steperValue);
    }
}
@end
